use crate::config::app_error::AppResult;
use crate::extension::index::index::indexer::DefaultIndexer;
use crate::extension::index::index::query::index_entry::IndexEntry;
use crate::extension::index::index::query::query::{BaseQuery, QueryIndexView};
use halo_model::{ExtensionOperator, GVK};
use ordermap::OrderSet;
use std::hash::Hash;

pub(crate) struct StringContains {
    pub field_name: String,
    pub value: String,
    pub is_field_ref: bool,
}

impl StringContains {
    pub fn new(field_name: String, value: String) -> Self {
        StringContains {
            field_name,
            value,
            is_field_ref: false,
        }
    }
}

impl<T: ExtensionOperator + GVK> BaseQuery for StringContains {
    fn matches(
        &self,
        index_view: &QueryIndexView<DefaultIndexer>,
    ) -> AppResult<OrderSet<String>> {
        let mut result_set = OrderSet::new();
        let index_entry = index_view.get_index_entry(&self.field_name.clone())?;
        for (k, v) in index_entry.borrow().entries() {
            if k.to_lowercase() == self.value {
                result_set.insert(v.clone());
            }
        }
        Ok(result_set)
    }
}
